/*
 * server.cpp
 *
 *  Created on: Apr 8, 2011
 *      Author: srkoh87z
 */

#include <stdio.h>
#include "ngine/ngine.h"

void handleMessage(NGMessage *msg);
void printMessage(NGMessage *msg);

int main()
{
	printf("Starting..\n");
	NGineServer::init(5000, 63, 0);
	printf("Started\n\n");

	while(1)
	{
		if(NGineServer::getQueueSize() > 0)
		{
			NGMessage *msg = NGineServer::getFirstMessage();
			handleMessage(msg);
		}
	}

	return 0;
}

void handleMessage(NGMessage *msg)
{
	printMessage(msg);

	if(strlen(msg->getData()) >= 5)
		NGineServer::sendMessage(1, "you've sent a message longer than 4 bytes!", msg->getSenderId());
	else
		NGineServer::sendMessage(2, "cutie!", msg->getSenderId());

	delete msg;
}

void printMessage(NGMessage *msg)
{
	printf("---------------------------------------------------------------------\n");
	printf("MSG received from %lu: [%d] %s\n", msg->getSenderId(), msg->getType(), msg->getData());
	printf("---------------------------------------------------------------------\n");
}
